package com.foreknow.demo9;

import java.io.*;

public class EmployeeObjectStream {
    public static void main(String[] args) {

//        try {
//            Employee e1 = new Employee("张三",20,"市场部");
//            Employee e2 = new Employee("李四",25,"开发部");
//            //使用ObjectOutputStream输出流向文件中写对象
//            FileOutputStream fos = new FileOutputStream("data.ser");
//            ObjectOutputStream oos = new ObjectOutputStream(fos);
//            //向文件中写对象
//            oos.writeObject(e1);
//            oos.writeObject(e2);
//            //关闭资源
//            oos.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
            FileInputStream fis = new FileInputStream("data.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            //从文件中读取对象
            Employee e1 = (Employee) ois.readObject();
            Employee e2 = (Employee) ois.readObject();
            e1.showInfo();
            e2.showInfo();
            //关闭资源
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
